package com.yuncheng.spcyApi.controller;


import cn.hutool.core.date.DateUtil;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.busService.MyJbxxService;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.mapper.*;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.service.impl.YcSysUserServiceImpl;
import com.yuncheng.spcyApi.spcyService.ZdyUploadFileService;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.spcyApi.vo.common.DeptCodeVo;
import com.yuncheng.spcyApi.vo.common.DeptUserVo;
import com.yuncheng.spcyApi.vo.common.StartEndTimeVo;
import com.yuncheng.spcyApi.vo.common.TreeVo;
import com.yuncheng.spcyApi.vo.jcy.DpxxAreaVo;
import com.yuncheng.spcyApi.vo.jcy.DpxxVo;
import com.yuncheng.spcyApi.vo.jcy.RsHcyVo;
import com.yuncheng.spcyApi.vo.ndkp.KhxxVo;
import com.yuncheng.spcyApi.vo.ndkp.NdkpFlowVo;
import com.yuncheng.spcyApi.vo.spcy.SpcyFlowVo;
import com.yuncheng.spcyApi.vo.sqsx.MyXpjlVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "系统-个人信息接口")
@RestController
@RequestMapping("/api/spzx-spcy/myJbxx")
public class MyJbxxController {
    private static final Logger log = LoggerFactory.getLogger(MyJbxxController.class);

    @Resource
    private YcSysUserServiceImpl ycSysUserService;

    @Resource
    private ISpcyRsHcyService spcyRsHcyService;

    @Resource
    private SpcySqsxXpjlMapper spcySqsxXpjlMapper;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private MyJbxxService myJbxxService;

    @Resource
    private MyExcelUtils myExcelUtils;

    @Resource
    @Lazy
    private YcSysOrgMapper ycSysOrgMapper;

    @Resource
    private ZdyUploadFileService zdyUploadFileService;

    @Log(title = "获取我的基本信息数据")
    @ApiOperation(value = "获取我的基本信息数据")
    @GetMapping(value = "queryMyJbxx")
    public HttpResult<?> queryMyJbxx(@RequestParam(value = "fuserid",defaultValue = "")String fuserid){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("登录已失效，请重新登录");
        }
        String userid = "";
        if (StringUtils.isBlank(fuserid)) {
            userid = currentUser.getId();
        }else {
            userid = fuserid;
        }
        Map map = new HashMap();

        map = myJbxxService.queryMyJbxx(userid);
        return HttpResult.ok(map);
    }

    @Log(title = "获取我的选派记录")
    @ApiOperation(value = "获取我的选派记录")
    @GetMapping(value = "queryMyXpjl")
    public HttpResult<?> queryMyXpjl(MyXpjlVo myXpjlVo,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("登录已过期，请重新登录");
        }
        if (StringUtils.isBlank(myXpjlVo.getFuserid())) {
            myXpjlVo.setFuserid(currentUser.getId());
        }
        IPage<MyXpjlVo> xpjlPageList = myJbxxService.queryMyXpjl(myXpjlVo, pageNo, pageSize);

        return HttpResult.ok(xpjlPageList);
    }

    /**
     * 获取我的考核信息
     */
    @Log(title = "获取我的考核信息")
    @ApiOperation(value = "获取我的考核信息")
    @GetMapping(value = "queryMyKhxx")
    public HttpResult<?> queryMyKhxx(@RequestParam(value = "fuserid",defaultValue = "")String fuserid,
                                     @RequestParam(value = "year",defaultValue = "")String year){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("登录已失效，请重新登录");
        }
        List<KhxxVo> khxxVoList = new ArrayList<>();
        String userid = "";
        if (StringUtils.isBlank(fuserid)){
            userid = currentUser.getId();
        }else {
            userid = fuserid;
        }
        khxxVoList = myJbxxService.queryMyKhxx(year,userid);

        return HttpResult.ok(khxxVoList);
    }

    /**
     * 获取我的调派信息
     * @return
     */
    @Log(title = "获取我的调派信息")
    @ApiOperation(value = "获取我的调派信息")
    @GetMapping(value = "queryMyDpxx")
    public HttpResult<?> queryMyDpxx(@RequestParam(value = "fuserid",defaultValue = "")String fuserid,
                                     @RequestParam(value = "year",defaultValue = "")String year){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("登录已失效，请重新登录");
        }
        // 获取当前年份
        if (StringUtils.isBlank(year)) {
            Date date = DateUtil.date();
            //获得年的部分
            year = DateConversionUtils.DateToYear(date);
        }
        String userid = "";
        String username = "";
        if (StringUtils.isBlank(fuserid)){
            userid = currentUser.getId();
            username = currentUser.getName();
        }else {
            userid = fuserid;
            YcSysUser user = ycSysUserService.getById(fuserid);
            if (user != null){
                username = user.getRealname();
            }
        }

        List<DpxxVo> dpxxVoList = new ArrayList<>();
        dpxxVoList = myJbxxService.queryMyDpxx(year, userid,username);
        return HttpResult.ok(dpxxVoList);
    }

    @Log(title = "获取核查员的选派信息-通过核查员id")
    @ApiOperation(value = "获取核查员的选派信息-通过核查员id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "核查员id",required = true),
            @ApiImplicitParam(name = "year",value = "年份（可不传，默认是今年）",required = true)
    })
    @GetMapping(value = "queryHcyXpxxByUserId")
    public HttpResult<?> queryHcyXpxxByUserId(@RequestParam(value = "id")String id,
                                              @RequestParam(value = "year",defaultValue = "")String year){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("主要参数丢失，请联系管理员");
        }
        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setId(id);
        List<RsHcyVo> rsHcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);
        if (CollectionUtils.isEmpty(rsHcyList)){
            return HttpResult.error("当前核查员不存在，请刷新重试");
        }
        RsHcyVo hcy = rsHcyList.get(0);

        // 获取当前年份
        if (StringUtils.isBlank(year)) {
            Date date = DateUtil.date();
            //获得年的部分
            year = DateConversionUtils.DateToYear(date);
        }
        Map resultMap = new HashMap();

        // 核查员基本信息
        Map map = myJbxxService.queryMyJbxx(hcy.getFuserid());
        resultMap.putAll(map);

        return HttpResult.ok(resultMap);
    }

    @Log(title = "查询核查员地域选派次数")
    @ApiOperation(value = "查询核查员地域选派次数")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "fuserid",value = "用户id",required = true),
            @ApiImplicitParam(name = "year",value = "年份（可不传，默认是今年）",required = true)
    })
    @GetMapping(value = "queryHcyAreaXpjlStat")
    public HttpResult<?> queryHcyAreaXpjlStat(DpxxAreaVo dpxxAreaVo){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if(currentUser == null){
            return HttpResult.error("登录已失效，请重新登录");
        }
        if (StringUtils.isBlank(dpxxAreaVo.getFuserid())){
            dpxxAreaVo.setFuserid(currentUser.getId());
        }
        if (StringUtils.isBlank(dpxxAreaVo.getYear())){
            String year = DateConversionUtils.DateToYear(new Date());
            dpxxAreaVo.setYear(year);
        }
        DpxxAreaVo dpxxArea = spcySqsxXpjlMapper.getHcyAreaXpjlStat(dpxxAreaVo);
        dpxxArea.setYear(dpxxAreaVo.getYear());
        if (StringUtils.isNotBlank(dpxxAreaVo.getFuserid())){
            YcSysUser user = ycSysUserService.getById(dpxxAreaVo.getFuserid());
            if (user != null){
                dpxxArea.setFjcyxm(user.getRealname());
            }
        }
        return HttpResult.ok(dpxxArea);
    }

    @Log(title = "更新用户信息")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    @ApiOperation(value = "更新用户信息")
    @PostMapping(value = "updateUser")
    public HttpResult<?> updateUser(@RequestBody HashMap<String,String> buffer) throws InvocationTargetException, IllegalAccessException {
        ycSysUserService.updateSysUser(buffer);
        return HttpResult.ok("保存成功");
    }

    @Log(title = "更新核查员信息")
    @ApiOperation(value = "更新核查员信息")
    @ApiImplicitParam(name = "id",value = "核查员id",required = true)
    @PostMapping(value = "updateHcy")
    public HttpResult<?> updateHcy(@RequestBody HashMap<String,String> buffer){
        String id = buffer.get("id");
        if (StringUtils.isBlank(id)){
            return HttpResult.error("核查员主要参数丢失，请联系管理员");
        }
        SpcyRsHcy hcy = spcyRsHcyService.getById(id);
        if (hcy == null){
            return HttpResult.error("保存失败，该核查员数据不存在");
        }
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        Map<String, Object> map = ObjectToMapUtils.objectToMap(hcy);
        try {
            BeanUtils.populate(hcy,buffer);
        } catch (IllegalAccessException e) {
            log.error("更新核查员信息出错【IllegalAccessException】"+e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新核查员信息出错【InvocationTargetException】"+e.getMessage());
        }
        spcyRsHcyService.updateById(hcy);

        String logNr = "修改个人信息: ";
        logNr += LogUtils.BeanModifyMsg(hcy,map);

        spcyLogService.addLogs(LogConstant.LOGS_RS_HCY,logNr,id,LogConstant.LOGS_RS_HCY,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("保存成功");
    }

    @Log(title = "导出我的检查信息")
    @ApiOperation(value = "导出我的检查信息")
    @ApiImplicitParam(name = "fuserid",value = "用户id",required = true)
    @GetMapping(value = "exportMyJcxx")
    public void exportMyJcxx(MyXpjlVo myXpjlVo,
                             @RequestParam(value = "isExportMe",defaultValue = "true")boolean isExportMe,
                             HttpServletResponse response){
        if (isExportMe) {
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            if (currentUser == null) {
                log.error("登录已过期，请重新登录");
                return;
            }
            if (StringUtils.isBlank(myXpjlVo.getFuserid())){
                myXpjlVo.setFuserid(currentUser.getId());
            }
        }

       try {
           String name = "exportMyJcxx";
           response.setContentType("application/ms-excel;charset=UTF-8");
           response.setHeader("Content-Disposition", "attachment;filename="
                   .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
           OutputStream out = response.getOutputStream();

           List<Object> list = new ArrayList<>(); // 导出的数据

           List<MyXpjlVo> xpjlList = spcySqsxXpjlMapper.getXpjlList(myXpjlVo);
           // 条件导出
           if (CollectionUtils.isNotEmpty(xpjlList)){
               for (MyXpjlVo e : xpjlList){
                   list.add(e);
               }
           }
           myExcelUtils.outputListToExclFile(BusinessConstant.IMPORT_MY_JCXX,list,out); // 导出模板数据成excel
       }catch (Exception e){
           log.error("导出我的检查信息数据出错-{}",e.getMessage());
       }
    }

    @Log(title = "导出我的选派记录")
    @ApiOperation(value = "导出我的选派记录")
    @ApiImplicitParam(name = "fuserid",value = "用户id",required = true)
    @GetMapping(value = "exportMyXpjl")
    public void exportMyXpjl(MyXpjlVo myXpjlVo,
                             @RequestParam(value = "isExportMe",defaultValue = "true")boolean isExportMe,
                             HttpServletResponse response){
        if (isExportMe) {
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            if (currentUser == null) {
                log.error("登录已过期，请重新登录");
                return;
            }
            if (StringUtils.isBlank(myXpjlVo.getFuserid())){
                myXpjlVo.setFuserid(currentUser.getId());
            }
        }

        try {
            String name = "exportMyXpjl";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<Object> list = new ArrayList<>(); // 导出的数据

            List<MyXpjlVo> xpjlList = spcySqsxXpjlMapper.getXpjlList(myXpjlVo);
            // 条件导出
            if (CollectionUtils.isNotEmpty(xpjlList)){
                for (MyXpjlVo e : xpjlList){
                    list.add(e);
                }
            }
            myExcelUtils.outputListToExclFile(BusinessConstant.IMPORT_MY_XPJL,list,out); // 导出模板数据成excel
        }catch (Exception e){
            log.error("导入我的选派记录数据出错-{}",e.getMessage());
        }
    }

    /**
     *  获取个人相关-所有类型的流程待办
     */
    @Log(title = "获取个人相关-所有类型的流程待办")
    @ApiOperation(value = "获取个人相关-所有类型的流程待办")
    @GetMapping(value = "queryAllYwflFlowJob")
    public HttpResult<?> queryAllYwflFlowJob(){
        Map map = new HashMap(); // 结果集 map
        Map spcyMap = new HashMap(); // 审评查验流程 map
        Map rckpMap = new HashMap(); // 日常考评 map
        Map ybjcyMap = new HashMap(); // 预备检查员考评 map
        Map ndkpMap = new HashMap(); // 年度考评 map

        CurrentUserVo user = GetCurrentUserUtils.getCurrentUser();
        if (user == null){
            return HttpResult.error("请重新登录，Token已失效！");
        }
        boolean onlyCurrentUser = true; // 查询个人
        Integer pageNo = 1;
        Integer pageSize = 1000;
        Page<SpcyFlowVo> page = new Page<>(pageNo, pageSize);

        /*********************************************【  审评查验 - 个科室 - 流程待办】**************************************/
        // 药品注册 - 审评查验
//        spcyMap.putAll(myJbxxService.mySpcyByYpzcDb(page,onlyCurrentUser,user));

        // 药品生产 - 审评查验
//        spcyMap.putAll(myJbxxService.mySpcyByYpscDb(page,onlyCurrentUser,user));

        // 药品流通 - 审评查验
//        spcyMap.putAll(myJbxxService.mySpcyByYpltDb(page,onlyCurrentUser,user));

        // 医疗器械 - 审评查验
//        spcyMap.putAll(myJbxxService.mySpcyByYlqxDb(page,onlyCurrentUser,user));

        // 化妆品 - 审评查验
//        spcyMap.putAll(myJbxxService.mySpcyByHzpDb(page,onlyCurrentUser,user));

        /*********************************************【  日常考评 流程待办】**********************************************/
        // 日常考评
//        rckpMap.putAll(myJbxxService.myRckpWithDb(page,onlyCurrentUser,user));

        /*********************************************【  预备检查员 考评  流程待办】**************************************/
        // 预备检查员考评
//        ybjcyMap.putAll(myJbxxService.myYbjcyKpWithDb(pageNo,pageSize,onlyCurrentUser,user));

        /*********************************************【  年度考评 - 个科室 - 流程待办】**************************************/
        Page<NdkpFlowVo> page2 = new Page<>(pageNo, pageSize);

        // 药品注册-年度考评
//        ndkpMap.putAll(myJbxxService.myNdkpByYpzcDb(page2,onlyCurrentUser,user));

        // 药品生产-年度考评
//        ndkpMap.putAll(myJbxxService.myNdkpByYpscDb(page2,onlyCurrentUser,user));

        // 药品流通-年度考评
//        ndkpMap.putAll(myJbxxService.myNdkpByYpltDb(page2,onlyCurrentUser,user));

        // 医疗器械-年度考评
//        ndkpMap.putAll(myJbxxService.myNdkpByYlqxDb(page2,onlyCurrentUser,user));

        // 化妆品-年度考评
//        ndkpMap.putAll(myJbxxService.myNdkpByHzpDb(page2,onlyCurrentUser,user));

        map.put("spcy",spcyMap); // 审评查验
        map.put("rckp",rckpMap); // 日常考评
        map.put("ybjcy",ybjcyMap); // 预备检查员考评
        map.put("ndkp",ndkpMap); // 年度考评
//        map.put("zltxwj",myJbxxService.myZltxwjFlowDb(onlyCurrentUser,user)); // 质量体系文件流程

//        map.put("qycjsq",myJbxxService.mySpcyQycjsqFlowDb(onlyCurrentUser,user)); // 企业撤件申请流程

        return HttpResult.ok(map);
    }

    @Log(title = "按科室流程数据统计")
    @ApiOperation(value = "按科室流程数据统计")
    @GetMapping(value = "queryDeptFlowTjData")
    public HttpResult<?> queryDeptFlowTjData(StartEndTimeVo startEndTimeVo){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("登录已失效，请重新登录");
        }
        String deptName = currentUser.getDeptName();
        String ywfl = "";
        log.info("当前登录人所属部门：{}",deptName);

        boolean isSelect = false;
        String ywflByDeptName = SpcyConstant.getYwflByDeptName(deptName);
        if (ywflByDeptName.equals("0")){
            ywfl = SpcyConstant.YWFL_CODE_YPZC+","+
                    SpcyConstant.YWFL_CODE_YPSC+","+
                    SpcyConstant.YWFL_CODE_YPJY+","+
                    SpcyConstant.YWFL_CODE_YLQX+","+
                    SpcyConstant.YWFL_CODE_YPZCLC+","+
                    SpcyConstant.YWFL_CODE_HZP;
            isSelect = false;
        }else if (ywflByDeptName.contains(SpcyConstant.ORG_NAME_ZXZR_CODE)){
            ywfl = SpcyConstant.YWFL_CODE_YPZC+","+
                    SpcyConstant.YWFL_CODE_YPSC+","+
                    SpcyConstant.YWFL_CODE_YPJY+","+
                    SpcyConstant.YWFL_CODE_YLQX+","+
                    SpcyConstant.YWFL_CODE_YPZCLC+","+
                    SpcyConstant.YWFL_CODE_HZP;
            isSelect = true;
        }else {
            ywfl = SpcyConstant.YWFL_CODE_YPZC+","+
                    SpcyConstant.YWFL_CODE_YPSC+","+
                    SpcyConstant.YWFL_CODE_YPJY+","+
                    SpcyConstant.YWFL_CODE_YLQX+","+
                    SpcyConstant.YWFL_CODE_YPZCLC+","+
                    SpcyConstant.YWFL_CODE_HZP;
            isSelect = true;
        }

        Map mapResult = new HashMap();
        Map successMap = new HashMap();
        if (isSelect){
            if (StringUtils.isNotBlank(ywfl)){
                List<String> ywflCodeList = Arrays.asList(ywfl.split(","));
                for (String ywflcode : ywflCodeList){
                    successMap = myJbxxService.queryDeptFlowTjData(startEndTimeVo, ywflcode);
                    mapResult.put(ywflcode,successMap);
                }
            }else {
                successMap = myJbxxService.queryDeptFlowTjData(startEndTimeVo, ywflByDeptName);
                mapResult.put(ywflByDeptName,successMap);
            }
        }else {
            List<String> ywflCodeList = Arrays.asList(ywfl.split(","));
            for (String ywflcode : ywflCodeList){
                successMap = myJbxxService.queryDeptFlowTjData(startEndTimeVo, ywflcode);
                mapResult.put(ywflcode,successMap);
            }
        }

        return HttpResult.ok(mapResult);
    }

    @Log(title = "获取个人流程统计")
    @ApiOperation(value = "获取个人流程统计")
    @GetMapping(value = "queryMyFlowTjData")
    public HttpResult<?> queryMyFlowTjData(StartEndTimeVo startEndTimeVo,
                                           @RequestParam(value = "onlyCurrentUser",defaultValue = "true")boolean onlyCurrentUser){
        CurrentUserVo user = GetCurrentUserUtils.getCurrentUser();
        if (user == null){
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Map resultMap = new HashMap();

//        Map map = myJbxxService.queryMyFlowTjData(onlyCurrentUser, user);

        resultMap.put("myTj",null);
        return HttpResult.ok(resultMap);
    }

    @Log(title = "获取审评中心下的所有部门-树形结构")
    @ApiOperation(value = "获取审评中心下的所有部门-树形结构")
    @GetMapping(value = "querySpcyDeptWithTree")
    public HttpResult<?> querySpcyDeptWithTree(){
        List<TreeVo> treeList = new ArrayList<>();
        TreeVo treeVo = new TreeVo();
        treeVo.setKey("spcy01");
        treeVo.setTitle(SpcyConstant.Dept_Spcy_Name);

        // 审评中心 下 的 所有部门
        List<YcSysOrg> orgList = ycSysOrgMapper.selectList(new LambdaQueryWrapper<YcSysOrg>()
                .eq(YcSysOrg::getParentId, SpcyConstant.Dept_Spcy_Id)
                .orderByAsc(YcSysOrg::getOrgOrder));

        if (CollectionUtils.isNotEmpty(orgList)){
            List<TreeVo> childList = new ArrayList<>();
            int i = 0;
            TreeVo treeVo2 = null;
            for (YcSysOrg org : orgList){
                i = i + 1;
                treeVo2 = new TreeVo();
                treeVo2.setKey(String.valueOf(i));
                treeVo2.setTitle(org.getOrgName());
                childList.add(treeVo2);
            }
            treeVo.setChildren(childList);
        }
        treeList.add(treeVo);

        return HttpResult.ok(treeList);
    }

    @Log(title = "获取审评中心下的所有部门和相关人员-数组结构")
    @ApiOperation(value = "获取审评中心下的所有部门和相关人员-数组结构")
    @GetMapping(value = "querySpcyDeptUserWithList")
    public HttpResult<?> querySpcyDeptUserWithList(@RequestParam(value = "deptName",defaultValue = "")String deptName){

        Map resultMap = new HashMap();

        // 审评中心 下 的 所有部门
        List<YcSysOrg> orgList = ycSysOrgMapper.selectList(new LambdaQueryWrapper<YcSysOrg>()
                .eq(YcSysOrg::getParentId, SpcyConstant.Dept_Spcy_Id)
                .orderByAsc(YcSysOrg::getOrgOrder));
        if (CollectionUtils.isNotEmpty(orgList)){
            // 部门名称数组
            List<String> deptList = new ArrayList<>();
            List<Map> deptUserMapList = new ArrayList<>();

            for (YcSysOrg org : orgList){
                String orgId = org.getId();
                String orgName = org.getOrgName();

                Map deptMap = new HashMap();
                List<Map> userMapList = new ArrayList<>();

                if (orgId.equals(SpcyConstant.JCFJ_ORG_ID)
                        || orgId.equals(SpcyConstant.HCY_ORGID)
                        || orgId.equals(SpcyConstant.HCY_ZJ_ORGID)
                        || orgId.equals(SpcyConstant.QY_ORG_ID)){
                    // 不处理这些部门
                }else {
                    deptMap.put("dept",orgName);
                    deptList.add(orgName);

                    // 对应部门下所有用户
                    /*List<YcSysUser> deptUserList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                            .eq(YcSysUser::getDelFlag, 0)
                            .eq(YcSysUser::getOrgId, orgId)
                            .orderByAsc(YcSysUser::getUserOrder)
                            .orderByAsc(YcSysUser::getCreateTime));*/
                    List<YcSysUser> deptUserList = ycSysUserService.getUserListByDeptId(orgId);

                    if (CollectionUtils.isNotEmpty(deptUserList)){
                        for (YcSysUser user : deptUserList){
                            Map userMap = new HashMap();
                            userMap.put("id",user.getId());
                            userMap.put("name",user.getRealname());
                            userMapList.add(userMap);
                        }
                    }
                    deptMap.put("userList",userMapList);
                    deptUserMapList.add(deptMap);
                }
            }
            resultMap.put("deptUserList",deptUserMapList);
            resultMap.put("deptList", deptList);
        }

        return HttpResult.ok(resultMap);
    }

    @Log(title = "获取审评中心下的核查员部门-树形结构")
    @ApiOperation(value = "获取审评中心下的核查员部门-树形结构")
    @GetMapping(value = "querySpcyDeptHcyWithTree")
    public HttpResult<?> querySpcyDeptHcyWithTree(){
        List<TreeVo> treeList = new ArrayList<>();
        TreeVo treeVo = new TreeVo();
        treeVo.setKey("spcy01");
        treeVo.setTitle(SpcyConstant.Dept_Spcy_Name);

        // 核查员
        List<TreeVo> childList = new ArrayList<>();
        TreeVo treeVo2 = new TreeVo();
        treeVo2.setKey("hcy01");
        treeVo2.setTitle(SpcyConstant.HCY_ORGNAME);
        childList.add(treeVo2);
        treeVo.setChildren(childList);

        treeList.add(treeVo);

        return HttpResult.ok(treeList);
    }

    @Log(title = "获取审评中心下的专家部门-树形结构")
    @ApiOperation(value = "获取审评中心下的专家部门-树形结构")
    @GetMapping(value = "querySpcyDeptZjWithTree")
    public HttpResult<?> querySpcyDeptZjWithTree(){
        List<TreeVo> treeList = new ArrayList<>();
        TreeVo treeVo = new TreeVo();
        treeVo.setKey("spcy01");
        treeVo.setTitle(SpcyConstant.Dept_Spcy_Name);

        // 核查员
        List<TreeVo> childList = new ArrayList<>();
        TreeVo treeVo2 = new TreeVo();
        treeVo2.setKey("zj01");
        treeVo2.setTitle(SpcyConstant.HCY_ZJ_ORGNAME);
        childList.add(treeVo2);
        treeVo.setChildren(childList);

        treeList.add(treeVo);

        return HttpResult.ok(treeList);
    }

    @Log(title = "通过部门名称-查询用户")
    @ApiOperation(value = "通过部门名称-查询用户")
    @GetMapping(value = "queryUserWithOrgName")
    public HttpResult<?> queryUserWithOrgName(DeptUserVo deptUserVo){
        List<DeptUserVo> deptUserList = ycSysOrgMapper.getDeptUserList(deptUserVo);
        List<Map> resultMapList = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(deptUserList)){
            Map map = null;
            for (DeptUserVo u : deptUserList){
                map = new HashMap();
                StringBuffer str = new StringBuffer();
                str.append(u.getRealname()).append("(");
                if (StringUtils.isNotBlank(u.getPhone())){
                    str.append(u.getPhone());
                }
                str.append(")");
                if (StringUtils.isNotBlank(u.getFgzdw())){
                    str.append(u.getFgzdw());
                }

                map.put("id",u.getId());
                map.put("name",str.toString());
                resultMapList.add(map);
            }
        }

        return HttpResult.ok(resultMapList);
    }

    @Log(title = "查询部门编码")
    @ApiOperation(value = "查询部门编码")
    @GetMapping(value = "queryDeptCode")
    public HttpResult<?> queryDeptCode(){
        List<YcSysOrg> orgList = ycSysOrgMapper.selectList(new LambdaQueryWrapper<YcSysOrg>()
                .isNotNull(YcSysOrg::getParentId)
                .orderByAsc(YcSysOrg::getOrgOrder));
        List<DeptCodeVo> codeList = orgList.stream()
                .map(f -> new DeptCodeVo(f.getOrgName(), f.getOrgCode())).collect(Collectors.toList());

        return HttpResult.ok(codeList);
    }

    @Log(title = "获取当前角色门户首页路径")
    @ApiOperation(value = "获取当前角色门户首页路径")
    @GetMapping(value = "queryNowUserRoleMhpz")
    public HttpResult<?> queryNowUserRoleMhpz(){
        Map resultMap = new HashMap();
        String url = myJbxxService.queryNowUserRoleMhpz();

        resultMap.put("url",url);
        return HttpResult.ok(resultMap);
    }

    @Log(title = "获取当前用户角色的水印名称")
    @ApiOperation(value = "获取当前用户角色的水印名称")
    @GetMapping(value = "getWaterMarkByUserRole")
    public HttpResult getWaterMarkByUserRole(){
        String symc = "";

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.ok(symc);
        }

        // 当前部门
        String orgName = currentUser.getOrgName();

        // 专家/检查分局 - 提供水印
        if (orgName.equals(SpcyConstant.HCY_ZJ_ORGNAME)){
            symc = BusinessConstant.FILE_SY_NAME;
        }else if (orgName.equals(SpcyConstant.JCFJ_ORG_NAME)){
            symc = BusinessConstant.FILE_SY_NAME;
        }else {
            symc = BusinessConstant.FILE_SY_NAME;
        }

        return HttpResult.ok(symc);
    }

    @Log(title = "用户上传签名")
    @ApiOperation(value = "用户上传签名")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    @PostMapping(value = "uploadMyQm")
    public HttpResult<?> uploadMyQm(@RequestParam("file") MultipartFile file,
                                    @RequestParam(value = "id",defaultValue = "")String id){

        zdyUploadFileService.uploadMyQm(file, id);
        return HttpResult.ok("上传签名成功");
    }

    @ApiOperation(value = "下载个人签名")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    @GetMapping(value = "downMyQm")
    public void downMyQm(@RequestParam(value = "id",defaultValue = "")String id, HttpServletResponse response){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            log.error("登录已失效，请重新登录");
            return;
        }

        zdyUploadFileService.downMyQm(id, currentUser, response);
    }

    /** 清空签名-根据文件id */
    @GetMapping(value = "emptyJcyQm")
    public HttpResult emptyJcyQm(@RequestParam(value = "id",defaultValue = "") String id,
                                 @RequestParam(value = "userid",defaultValue = "") String userid){

        zdyUploadFileService.deleteMyQm(id, userid);

        return HttpResult.ok();
    }

}
